package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Yang
 * @Date 2023/3/5 10:58
 */
@Service
@Transactional
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //分页查询,并携带菜品名
    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //分页的条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Dish::getName, name);
        qw.orderByDesc(Dish::getUpdateTime);
        //构建分页
        Page<Dish> p = new Page<>(pageNum, pageSize);
        //分页查询
        p = dishMapper.selectPage(p, qw);
        //遍历菜品
        for (Dish dish : p.getRecords()) {
            //查询分类 select * from category where id = #{id}
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setCategoryName(category.getName());

            //查询口味列表 select * from dish_flavor where dish_id = #{id}
            LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DishFlavor::getDishId, dish.getId()); // where dish_id = id
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(qw2);
            dish.setFlavors(dishFlavors);
        }
        return p;
    }

    //新增
    @Override
    public void save(Dish dish) {
        // 将菜品保存
        dishMapper.insert(dish);

        // 2.取出前端提供口味列表并遍历
        List<DishFlavor> flavorList = dish.getFlavors();
        for (DishFlavor dishFlavor : flavorList) {
            // 2-1 关联菜品id
            dishFlavor.setDishId(dish.getId());
            // 2-2 保存数据库  insert into dish_flavor() values();
            dishFlavorMapper.insert(dishFlavor);
        }
    }

    //ID回显
    @Override
    public Dish findById(Long dishId) {
        Dish dish = dishMapper.selectById(dishId);
        // 2.构建口味查询条件 select * from dish_flavor where dish_id = 1632199055591378946
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dishId);
        // 3.查询口味列表
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(qw);
        // 4.将口味封装到菜品
        dish.setFlavors(dishFlavors);

        return dish;
    }

    //修改
    @Override
    public void update(Dish dish) {
        //先修改菜品信息
        dishMapper.updateById(dish);
        // 2.根据菜品id删除旧口味  delete from dish_flavor where dish_id = 1
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dish.getId());// where dish_id = 1
        dishFlavorMapper.delete(qw);

        //查看前端传来的口味储存
        for (DishFlavor flavor : dish.getFlavors()) {
            //生成id
            flavor.setDishId(dish.getId());
            //储存口味
            dishFlavorMapper.insert(flavor);
        }
    }

    //修改起售状态
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                Dish dish = new Dish();
                dish.setId(id);
                dish.setStatus(status);
                dishMapper.updateById(dish);
            }
        }
    }

    //通过id批量删除
    @Override
    public void deleteByIds(List<Long> ids) {
        //如果ids不为空则直接删除
        LambdaQueryWrapper<Dish> qw1 = new LambdaQueryWrapper<>();
        //设置删除的条件
        qw1.in(Dish::getId,ids);
        qw1.eq(Dish::getStatus,1);
        //如果查询的条件,查到数据则不删除返回错误
        Integer integer = dishMapper.selectCount(qw1);
        if(integer > 0){
            throw new CustomException("有起售的菜品,无法删除");
        }

        //关联第三章表
        LambdaQueryWrapper<SetmealDish> qw2 = new LambdaQueryWrapper<>();
        qw2.in(SetmealDish::getDishId ,ids);
        Integer count2= setmealDishMapper.selectCount(qw2);
        //如果删除的菜品，有套餐关联，那么不允许删除
        if(count2 > 0){
            throw new CustomException("删除的菜品有套餐关联，不允许删除");
        }

        //如果走到这里就证明没有套餐,并且没有起售状态,直接删除
        dishMapper.deleteBatchIds(ids);

        //删除口味
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.in(DishFlavor::getDishId,ids);
        dishFlavorMapper.delete(qw);
    }

    //新增套餐时回显菜品
    @Override
    public List<Dish> findListBySetmeal(Long categoryId, String name) {
        //构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //如果categoryid是null就执行模糊查询
        qw.eq(categoryId != null,Dish::getCategoryId,categoryId);
        //如果name是null就执行查询菜品
        qw.like(StrUtil.isNotEmpty(name),Dish::getName,name);
        List<Dish> dishes = dishMapper.selectList(qw);
        return dishes;
    }

    @Override
    public List<Dish> list(Long categoryId, Integer status) {
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);
        qw.eq(Dish::getStatus,status);
        //获取分类下所有菜品
        List<Dish> dishes = dishMapper.selectList(qw);

        for (Dish dish : dishes) {
            LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(qw2);

            dish.setFlavors(dishFlavors);
        }


        return dishes;
    }


}

















